package cn.iocoder.yudao.module.book.dal.mysql.homeworks;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.book.dal.dataobject.homeworks.HomeworksDO;
import cn.iocoder.yudao.module.book.controller.app.homeworks.vo.AppHomeworkDetailRespVO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.book.controller.admin.homeworks.vo.*;

/**
 * 作业 Mapper
 *
 * @author xi
 */
@Mapper
public interface HomeworksMapper extends BaseMapperX<HomeworksDO> {

    default PageResult<HomeworksDO> selectPage(HomeworksPageReqVO reqVO) {
        LambdaQueryWrapperX<HomeworksDO> query = new LambdaQueryWrapperX<HomeworksDO>()
                .eqIfPresent(HomeworksDO::getHomeworkId, reqVO.getHomeworkId())
                .eqIfPresent(HomeworksDO::getClassesId, reqVO.getClassesId())
                .eqIfPresent(HomeworksDO::getTitle, reqVO.getTitle())
                .likeIfPresent(HomeworksDO::getRequirement, reqVO.getRequirement())
                .likeIfPresent(HomeworksDO::getDescription, reqVO.getDescription())
                .betweenIfPresent(HomeworksDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(HomeworksDO::getHomeworkId);

        // date 查询：单值等值匹配，双值区间匹配
        java.time.LocalDate[] dateRange = reqVO.getDate();
        if (dateRange != null) {
            java.time.LocalDate start = cn.iocoder.yudao.framework.common.util.collection.ArrayUtils.get(dateRange, 0);
            java.time.LocalDate end = cn.iocoder.yudao.framework.common.util.collection.ArrayUtils.get(dateRange, 1);
            if (start != null && end != null) {
                query.between(HomeworksDO::getDate, start, end);
            } else if (start != null) {
                query.eq(HomeworksDO::getDate, start);
            } else if (end != null) {
                query.eq(HomeworksDO::getDate, end);
            }
        }

        return selectPage(reqVO, query);
    }

    default List<HomeworksDO> selectByClassId(Integer classId) {
        return selectList(new LambdaQueryWrapperX<HomeworksDO>()
                .eq(HomeworksDO::getClassesId, classId)
                .orderByDesc(HomeworksDO::getCreateTime));
    }

    /**
     * 查询作业详情及关联的绘本集
     */
    AppHomeworkDetailRespVO selectHomeworkWithCollections(Integer homeworkId);

}